//
//  WPTopupViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/1.
//  Copyright © 2016年 HuaYiSoftware. All rights reserved.
//

#import "WPTopupViewController.h"
#import "PayPlug.h"

@interface WPTopupViewController ()
@property (nonatomic, strong) UITextField * moneyCountText;/**< 充值金额 */
@property (nonatomic, strong) UIButton * topupBtn;/**< 充值按钮 */
- (void)initializeUserInterface; /**< 初始化用户界面 */


@end

@implementation WPTopupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeUserInterface {
    [self.view addSubview:self.moneyCountText];
    [self.view addSubview:self.topupBtn];
}
#pragma mark - responds events
- (void)respondsToTopupBtn {
    [self.view endEditing:YES];
    NSString * alipayCount = [self configMoneyCount];
    if ([alipayCount isEqualToString:@"0.00"]) {
        [self initializeAlertControllerWithMessage:@"亲，金额太少不能充值"];
        return;
    }
    __weak typeof(self)weakself = self;
    PayPlug *pay = [PayPlug sharedInstance];
    pay.payWay = AliPay;
    //        pay.sn = [self generateTradeNOWithStyle:YES];
    pay.sn = @"231";
    pay.productDesc = @"五洲购钱包预充值";
    pay.productPrice = @"0.01";
    pay.productTitle = @"五洲购充值";
    pay.notiUrl = @"http://121.41.47.43:81";
    [pay doPay:^(PayWay way, id obj, NSError *error) {
        [weakself initializeAlertControllerWithMessageAndDismiss:obj];
    }];
}
#pragma mark - 获取充值金额
- (NSString *)configMoneyCount {
    NSString * originStr = self.moneyCountText.text;
    CGFloat moneycount = [originStr floatValue];
    NSString * resultStr = [NSString string];
    resultStr = [NSString stringWithFormat:@"%.2f", moneycount];
    return resultStr;
}
#pragma mark - 充值订单生成
- (NSString *)generateTradeNOWithStyle:(BOOL)style
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSDate * date = [NSDate date];
    NSMutableString * timeStr = [NSMutableString stringWithString:[dateFormatter stringFromDate:date]];
    if (timeStr.length < 14) {
        for (int i = 0; i < 14 - timeStr.length; i++) {
            [timeStr insertString:@"0" atIndex:timeStr.length];
        }
    } else {
        [timeStr substringToIndex:14];
    }
    NSString * basicNO = [NSString string];
    if (style) {
        basicNO = [NSString stringWithFormat:@"AliPay%@%@", timeStr, [UserModel defaultUser].userID];
    } else {
        basicNO = [NSString stringWithFormat:@"WeiXin%@%@", timeStr, [UserModel defaultUser].userID];
    }
    if (basicNO.length > 32) {
        return [basicNO substringFromIndex:basicNO.length -  32];
    } else {
        return basicNO;
    }
}

#pragma mark - getter
- (UITextField *)moneyCountText {
    if (!_moneyCountText) {
        _moneyCountText = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, navBar_Fheight+10, SCREEN_SIZE.width, 40)];
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 40) adjustWidth:NO];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 90, 40) adjustWidth:NO];
            [leftView addSubview:lab];
            text.leftViewMode = UITextFieldViewModeAlways;
            text.leftView = leftView;
            lab.text = @"充值金额(元)：";
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(141, 141, 141, 1);
            text.backgroundColor = [UIColor whiteColor];
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.placeholder = @"（请输入充值金额）";
            text.keyboardType = UIKeyboardTypeDecimalPad;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text;
        });
    }
    return _moneyCountText;
}
- (UIButton *)topupBtn {
    if (!_topupBtn) {
        _topupBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.moneyCountText.frame) + 40*DHFlexibleHorizontalMutiplier(), 300*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 15*DHFlexibleHorizontalMutiplier();
            btn.titleLabel.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"充值" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToTopupBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _topupBtn;
}
@end
